package com.foo.game.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.foo.common.base.action.FooGenericAction;
import com.foo.common.base.pojo.FooGenericSearch;
import com.foo.common.base.pojo.FooGenericSearchResult;
import com.foo.common.base.utils.FooUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

public class GameAction extends FooGenericAction {
	private static final long serialVersionUID = 1L;

	@org.apache.struts2.convention.annotation.Action("/game/list")
	public void list() throws IOException {
		Map<String, Object> myMap = Maps.newHashMap();

		FooGenericSearch search = new FooGenericSearch();
		search.setQueryHql(" from Game");
		search.setCountHql(" select count(*) from Game");
		List<?> myList = null;
		FooGenericSearchResult fooGenericSearchResult = null;

		if (Strings.nullToEmpty(request.getParameter("bNeedPaging")).equals(
				"false")) {
			fooGenericSearchResult = fooGenericService.list(search);
			myList = fooGenericSearchResult.getResult();
			myMap.put("aaData", myList);
			int myCount = fooGenericSearchResult.getTotalCount();
			myMap.put("iTotalRecords", myCount);
			myMap.put("iTotalDisplayRecords", myCount);
		} else {
			String iDisplayLength = request.getParameter("iDisplayLength");
			String iDisplayStart = request.getParameter("iDisplayStart");
			search.setFirstResult(Integer.parseInt(iDisplayStart));
			search.setMaxResults(Integer.parseInt(iDisplayLength));
			FooGenericSearchResult searchResult = fooGenericService
					.searchAndCount(search);
			myList = searchResult.getResult();
			myMap.put("aaData", myList);
			myMap.put("iTotalRecords", searchResult.getTotalCount());
			myMap.put("iTotalDisplayRecords", searchResult.getTotalCount());
		}
		FooUtils.printJsonObjectSerializeNulls(response, myMap);
	}

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.println(FooUtils.generateUUID());
		}
	}
}
